//
//  RNChildViewController.m
//  ModifyMetroDemo
//
//  Created by Marcus on 2020/3/8.
//  Copyright © 2020 Facebook. All rights reserved.
//

#import "RNChildViewController.h"
#import <React/RCTRootView.h>
#import "RCTBridge.h"
#import <React/RCTBridge+Private.h>
#import "AppDelegate.h"
#import "SharePerenceHelper.h"
#import "RNTimeUtils.h"

@interface RNChildViewController ()

@end

@implementation RNChildViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  // Do any additional setup after loading the view.
  self.businessInfo = [[SharePerenceHelper instance] getReactNativeBusinessInfo];
  NSString* title = [[NSString alloc] initWithFormat:@"Async: %@", self.businessInfo.displayName];
  [self.navigationItem setTitle:title ];
  [self initReactNativeContext];
}

- (void) initReactNativeContext{
  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(handleBridgeDidLoadJavaScriptNotification:)
                                               name:RCTContentDidAppearNotification
                                             object:nil];
  
  AppDelegate *myDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
  
  NSLog(@"subapp: 业务包加载开始 ");
  NSURL *businessDiffFile = [[NSBundle mainBundle] URLForResource: self.businessInfo.diffBundleFilePath
                                                    withExtension:@"bundle"];
  NSData * businessDiffData = [NSData dataWithContentsOfURL:businessDiffFile];
  
  [(RCTBridge *) myDelegate.bridge.batchedBridge executeSourceCode:businessDiffData sync:NO];
  NSLog(@"subapp: 业务包加载结束 ");
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:myDelegate.bridge
                                                   moduleName:self.businessInfo.mainComponentName
                                            initialProperties:nil];
  rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
  self.view = rootView;
}

- (void)handleBridgeDidLoadJavaScriptNotification:(NSNotification *)notification
{
  [RNTimeUtils.instance endTimeRecord];
  [RNTimeUtils.instance printTimeCost];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
